﻿using Pdoxcl2Sharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace V3MainTool.Model
{
    public class TechFamily : IParadoxRead, IParadoxWrite
    {
        public IList<Technology> MyList { get; set; }
        public TechFamily()
        {
            MyList = new List<Technology>();
        }

        public void TokenCallback(ParadoxParser parser, string token)
        {
            Technology item = parser.Parse(new Technology());
            item.name = token;
            MyList.Add(item);
        }

        public void Write(ParadoxStreamWriter writer)
        {
            for (int i = 0; i < MyList.Count; i++)
            {
                writer.Write(MyList[i].name, MyList[i]);
            }
        }
    }
    public class Technology : IParadoxRead, IParadoxWrite
    {
        #region 转换
        public string eraStr
        {
            get
            {
                switch (era)
                {
                    case "era_1": return "1 级";
                    case "era_2": return "2 级";
                    case "era_3": return "3 级";
                    case "era_4": return "4 级";
                    case "era_5": return "5 级";
                    default: return "未知";
                }
            }
        }
        public string nameStr
        {
            get
            {
                Dictionary<string, string> dic = LocalHelper.InventionsDic;
                if (dic.TryGetValue(name, out string val))
                {
                    return val;
                }
                return name;
            }
        }
        public string unlocking_technologiesStr
        {
            get
            {
                if (unlocking_technologies == null)
                {
                    return "";
                }
                if (unlocking_technologies.Count == 0)
                {
                    return "";
                }
                Dictionary<string, string> dic = LocalHelper.InventionsDic;
                string str = "";
                foreach (var item in unlocking_technologies)
                {
                    if (dic.TryGetValue(item, out string val))
                    {
                        str += val + ",";
                    }
                    else
                    {
                        str += item + ",";
                    }
                }

                return str.Substring(0, str.Length - 1);
            }
        }
        #endregion
        public string name { get; set; }
        public string era { get; set; }
        public string texture { get; set; }
        public string category { get; set; }
        public string can_research { get; set; }
        public IList<string> unlocking_technologies { get; set; }
        public Modifier modifier { get; set; }
        public void TokenCallback(ParadoxParser parser, string token)
        {
            switch (token)
            {
                case "era": era = parser.ReadString(); break;
                case "texture": texture = parser.ReadString(); break;
                case "category": category = parser.ReadString(); break;
                case "can_research": can_research = parser.ReadString(); break;
                case "unlocking_technologies": unlocking_technologies = parser.ReadStringList(); break;
                case "modifier": modifier = parser.Parse(new Modifier()); break;
                default:
                    Console.WriteLine("unknow Technology prop:  " + token);
                    break;
            }
        }

        public void Write(ParadoxStreamWriter writer)
        {
            if (era != null)
            {
                writer.WriteLine("era", era);
            }
            if (texture != null)
            {
                writer.WriteLine("texture", texture, ValueWrite.Quoted);
            }
            if (category != null)
            {
                writer.WriteLine("category", category, ValueWrite.Quoted);
            }
            if (can_research != null)
            {
                writer.WriteLine("can_research", can_research, ValueWrite.Quoted);
            }
            if (unlocking_technologies != null)
            {
                writer.Write("unlocking_technologies={ ");
                foreach (var val in unlocking_technologies)
                {
                    writer.Write(val, ValueWrite.Quoted);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (modifier != null)
            {
                writer.Write("modifier", modifier);
            }
        }
    }
    public class Modifier : IParadoxRead, IParadoxWrite
    {
        #region tech production
        public float? building_silk_plantation_throughput_mult { get; set; }
        public float? building_cotton_plantation_throughput_mult { get; set; }
        public float? country_resource_discovery_chance_mult { get; set; }
        public float? building_construction_sector_throughput_mult { get; set; }
        #endregion

        #region tech military
        public float? state_building_port_max_level_add { get; set; }
        public float? unit_army_defense_mult { get; set; }
        public float? state_conscription_rate_add { get; set; }
        public float? unit_kill_rate_add { get; set; }
        #endregion

        #region tech society
        public float? state_infrastructure_add { get; set; }
        public float? state_mortality_mult { get; set; }
        public float? state_expected_sol_from_literacy { get; set; }
        public float? country_minting_mult { get; set; }
        public float? country_institution_colonial_affairs_max_investment_add { get; set; }
        public float? country_trade_route_competitiveness_mult { get; set; }
        public float? country_prestige_mult { get; set; }
        public float? country_authority_mult { get; set; }
        public float? country_influence_mult { get; set; }
        public float? country_diplomatic_play_maneuvers_add { get; set; }
        #endregion

        public void TokenCallback(ParadoxParser parser, string token)
        {
            switch (token)
            {
                case "building_silk_plantation_throughput_mult":
                    building_silk_plantation_throughput_mult = parser.ReadFloat(); break;
                case "building_cotton_plantation_throughput_mult":
                    building_cotton_plantation_throughput_mult = parser.ReadFloat(); break;
                case "country_resource_discovery_chance_mult":
                    country_resource_discovery_chance_mult = parser.ReadFloat(); break;
                case "building_construction_sector_throughput_mult":
                    building_construction_sector_throughput_mult = parser.ReadFloat(); break;
                case "state_building_port_max_level_add":
                    state_building_port_max_level_add = parser.ReadFloat(); break;
                case "unit_army_defense_mult":
                    unit_army_defense_mult = parser.ReadFloat(); break;
                case "state_conscription_rate_add":
                    state_conscription_rate_add = parser.ReadFloat(); break;
                case "unit_kill_rate_add":
                    unit_kill_rate_add = parser.ReadFloat(); break;
                case "state_infrastructure_add":
                    state_infrastructure_add = parser.ReadFloat(); break;
                case "state_mortality_mult":
                    state_mortality_mult = parser.ReadFloat(); break;
                case "state_expected_sol_from_literacy":
                    state_expected_sol_from_literacy = parser.ReadFloat(); break;
                case "country_minting_mult":
                    country_minting_mult = parser.ReadFloat(); break;
                case "country_institution_colonial_affairs_max_investment_add":
                    country_institution_colonial_affairs_max_investment_add = parser.ReadFloat(); break;
                case "country_trade_route_competitiveness_mult":
                    country_trade_route_competitiveness_mult = parser.ReadFloat(); break;
                case "country_prestige_mult":
                    country_prestige_mult = parser.ReadFloat(); break;
                case "country_authority_mult":
                    country_authority_mult = parser.ReadFloat(); break;
                case "country_influence_mult":
                    country_influence_mult = parser.ReadFloat(); break;
                case "country_diplomatic_play_maneuvers_add":
                    country_diplomatic_play_maneuvers_add = parser.ReadFloat(); break;
                default:
                    Console.WriteLine("unknow modefier prop:  " + token);
                    break;
            }
        }

        public void Write(ParadoxStreamWriter writer)
        {
            if (building_silk_plantation_throughput_mult != null)
            {
                writer.WriteLine("building_silk_plantation_throughput_mult", building_silk_plantation_throughput_mult.Value);
            }
            if (building_cotton_plantation_throughput_mult != null)
            {
                writer.WriteLine("building_cotton_plantation_throughput_mult", building_cotton_plantation_throughput_mult.Value);
            }
            if (country_resource_discovery_chance_mult != null)
            {
                writer.WriteLine("country_resource_discovery_chance_mult", country_resource_discovery_chance_mult.Value);
            }
            if (building_construction_sector_throughput_mult != null)
            {
                writer.WriteLine("building_construction_sector_throughput_mult", building_construction_sector_throughput_mult.Value);
            }
            if (state_building_port_max_level_add != null)
            {
                writer.WriteLine("state_building_port_max_level_add", state_building_port_max_level_add.Value);
            }
            if (unit_army_defense_mult != null)
            {
                writer.WriteLine("unit_army_defense_mult", unit_army_defense_mult.Value);
            }
            if (state_conscription_rate_add != null)
            {
                writer.WriteLine("state_conscription_rate_add", state_conscription_rate_add.Value);
            }
            if (unit_kill_rate_add != null)
            {
                writer.WriteLine("unit_kill_rate_add", unit_kill_rate_add.Value);
            }
            if (state_infrastructure_add != null)
            {
                writer.WriteLine("state_infrastructure_add", state_infrastructure_add.Value);
            }
            if (state_mortality_mult != null)
            {
                writer.WriteLine("state_mortality_mult", state_mortality_mult.Value);
            }
            if (state_expected_sol_from_literacy != null)
            {
                writer.WriteLine("state_expected_sol_from_literacy", state_expected_sol_from_literacy.Value);
            }
            if (country_minting_mult != null)
            {
                writer.WriteLine("country_minting_mult", country_minting_mult.Value);
            }
            if (country_institution_colonial_affairs_max_investment_add != null)
            {
                writer.WriteLine("country_institution_colonial_affairs_max_investment_add", country_institution_colonial_affairs_max_investment_add.Value);
            }
            if (country_trade_route_competitiveness_mult != null)
            {
                writer.WriteLine("country_trade_route_competitiveness_mult", country_trade_route_competitiveness_mult.Value);
            }
            if (country_prestige_mult != null)
            {
                writer.WriteLine("country_prestige_mult", country_prestige_mult.Value);
            }
            if (country_authority_mult != null)
            {
                writer.WriteLine("country_authority_mult", country_authority_mult.Value);
            }
            if (country_influence_mult != null)
            {
                writer.WriteLine("country_influence_mult", country_influence_mult.Value);
            }
            if (country_diplomatic_play_maneuvers_add != null)
            {
                writer.WriteLine("country_diplomatic_play_maneuvers_add", country_diplomatic_play_maneuvers_add.Value);
            }
        }
    }
}
